package com.jiangyg.mall.cart.interceptor;

import com.jiangyg.mall.authz.MemberInfoHolder;
import com.jiangyg.mall.core.support.restful.Result;
import com.jiangyg.mall.core.utils.WebUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 类描述：调用购物车业务时，必须保证会员已登录
 *
 * @author jiangyg
 * @version 4.0
 * @date 2022-12-07
 */
public class CartInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取当前登录会员ID，如果不能获取到登录信息则返回异常
        final Long memberId = MemberInfoHolder.loginMemberId();
        if (memberId == null) {
            WebUtils.out(response, Result.instance("421"));
            return false;
        }
        return true;
    }

}
